/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package editor.gui;

import editor.domain.Mode;
import editor.domain.Polygon;
import editor.service.FileHandler;
import editor.service.TriangulationService;
import java.awt.CheckboxMenuItem;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.MenuItem;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFileChooser;

/**
 *
 * @author Daan
 */
public class EditorForm extends javax.swing.JFrame {

    private TriangulationService te;

    /**
     * Creates new form EditorForm
     */
    public EditorForm() {
        initComponents();
        this.te = new TriangulationService();
        panelGrid.setTriangulationEditor(te);
        btnDraw.setSelected(true);

//        this.addFocusListener(new FocusListener() {
//
//            @Override
//            public void focusGained(FocusEvent e) {
//                panelGrid.requestFocusInWindow();
//                panelGrid.requestFocus();
//                System.out.println(e.getComponent() + " - " + e.getOppositeComponent());
//            }
//
//            @Override
//            public void focusLost(FocusEvent e) {
//                
//                panelGrid.requestFocusInWindow();
//                panelGrid.requestFocus();
//            }
//        });

        KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addPropertyChangeListener(new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                panelGrid.requestFocusInWindow();
            }
        });
        
//        manager.addKeyEventDispatcher(new KeyEventDispatcher() {
//
//            @Override
//            public boolean dispatchKeyEvent(KeyEvent e) {
//                panelGrid.requestFocusInWindow();
//                return false;
//            }
//        });
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnDraw = new javax.swing.JToggleButton();
        btnSelect = new javax.swing.JToggleButton();
        btnLine = new javax.swing.JToggleButton();
        panelGrid = new editor.gui.DrawingPanel();
        lblCoordinates = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        menuFile = new javax.swing.JMenu();
        menuFileNew = new javax.swing.JMenuItem();
        menuFileOpen = new javax.swing.JMenuItem();
        menuFileSave = new javax.swing.JMenuItem();
        menuFileSaveAs = new javax.swing.JMenuItem();
        menuEdit = new javax.swing.JMenu();
        menuEditSelect = new javax.swing.JMenuItem();
        menuEditDeselect = new javax.swing.JMenuItem();
        menuEditDelete = new javax.swing.JMenuItem();
        menuFileHori = new javax.swing.JMenuItem();
        menuFileVerti = new javax.swing.JMenuItem();
        menuView = new javax.swing.JMenu();
        menuViewGrid = new javax.swing.JCheckBoxMenuItem();
        menuViewScale = new javax.swing.JCheckBoxMenuItem();
        menuZoom = new javax.swing.JMenu();
        menuZoomIn = new javax.swing.JMenuItem();
        menuZoomOut = new javax.swing.JMenuItem();
        menuTools = new javax.swing.JMenu();
        menuToolsAuto = new javax.swing.JCheckBoxMenuItem();
        menuToolsFill = new javax.swing.JCheckBoxMenuItem();
        menuToolsStick = new javax.swing.JCheckBoxMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Triangulation Editor");

        btnDraw.setText("Draw");
        btnDraw.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnDrawActionPerformed(evt);
            }
        });

        btnSelect.setText("Select");
        btnSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSelectActionPerformed(evt);
            }
        });

        btnLine.setText("Line");
        btnLine.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLineActionPerformed(evt);
            }
        });

        panelGrid.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                panelGridMouseMoved(evt);
            }
        });

        lblCoordinates.setText("x:- y:-");

        menuFile.setText("File");

        menuFileNew.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_N, java.awt.event.InputEvent.CTRL_MASK));
        menuFileNew.setText("New file..");
        menuFileNew.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileNewActionPerformed(evt);
            }
        });
        menuFile.add(menuFileNew);

        menuFileOpen.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_O, java.awt.event.InputEvent.CTRL_MASK));
        menuFileOpen.setText("Open file..");
        menuFileOpen.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileOpenActionPerformed(evt);
            }
        });
        menuFile.add(menuFileOpen);

        menuFileSave.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.CTRL_MASK));
        menuFileSave.setText("Save");
        menuFileSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileSaveActionPerformed(evt);
            }
        });
        menuFile.add(menuFileSave);

        menuFileSaveAs.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_S, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        menuFileSaveAs.setText("Save as..");
        menuFileSaveAs.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileSaveAsActionPerformed(evt);
            }
        });
        menuFile.add(menuFileSaveAs);

        jMenuBar1.add(menuFile);

        menuEdit.setText("Edit");

        menuEditSelect.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
        menuEditSelect.setText("Select all");
        menuEditSelect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEditSelectActionPerformed(evt);
            }
        });
        menuEdit.add(menuEditSelect);

        menuEditDeselect.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        menuEditDeselect.setText("Deselect all");
        menuEditDeselect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEditDeselectActionPerformed(evt);
            }
        });
        menuEdit.add(menuEditDeselect);

        menuEditDelete.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_DELETE, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
        menuEditDelete.setText("Delete all");
        menuEditDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuEditDeleteActionPerformed(evt);
            }
        });
        menuEdit.add(menuEditDelete);

        menuFileHori.setText("Flip horizontally");
        menuFileHori.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileHoriActionPerformed(evt);
            }
        });
        menuEdit.add(menuFileHori);

        menuFileVerti.setText("Flip vertically");
        menuFileVerti.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuFileVertiActionPerformed(evt);
            }
        });
        menuEdit.add(menuFileVerti);

        jMenuBar1.add(menuEdit);

        menuView.setText("View");

        menuViewGrid.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_G, java.awt.event.InputEvent.CTRL_MASK));
        menuViewGrid.setSelected(true);
        menuViewGrid.setText("Show grid");
        menuViewGrid.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuViewGridActionPerformed(evt);
            }
        });
        menuView.add(menuViewGrid);

        menuViewScale.setText("Show scale");
        menuViewScale.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuViewScaleActionPerformed(evt);
            }
        });
        menuView.add(menuViewScale);

        menuZoom.setText("Zoom");

        menuZoomIn.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ADD, 0));
        menuZoomIn.setText("Zoom in");
        menuZoomIn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuZoomInActionPerformed(evt);
            }
        });
        menuZoom.add(menuZoomIn);

        menuZoomOut.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_SUBTRACT, 0));
        menuZoomOut.setText("Zoom out");
        menuZoomOut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuZoomOutActionPerformed(evt);
            }
        });
        menuZoom.add(menuZoomOut);

        menuView.add(menuZoom);

        jMenuBar1.add(menuView);

        menuTools.setText("Tools");

        menuToolsAuto.setText("Auto-triangulate");
        menuToolsAuto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuToolsAutoActionPerformed(evt);
            }
        });
        menuTools.add(menuToolsAuto);

        menuToolsFill.setText("Fill polygons/triangles");
        menuToolsFill.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuToolsFillActionPerformed(evt);
            }
        });
        menuTools.add(menuToolsFill);

        menuToolsStick.setText("Stick to grid");
        menuToolsStick.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuToolsStickActionPerformed(evt);
            }
        });
        menuTools.add(menuToolsStick);

        jMenuBar1.add(menuTools);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(btnDraw)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnLine)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(btnSelect)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(lblCoordinates)
                .addContainerGap())
            .addComponent(panelGrid, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnDraw)
                    .addComponent(btnSelect)
                    .addComponent(btnLine)
                    .addComponent(lblCoordinates))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelGrid, javax.swing.GroupLayout.DEFAULT_SIZE, 504, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void menuFileNewActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFileNewActionPerformed
        te.setPolygon(new Polygon());
        FileHandler.clearOpenedFile();
        panelGrid.refresh();
    }//GEN-LAST:event_menuFileNewActionPerformed

    private void menuFileOpenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFileOpenActionPerformed
        te.setPolygon(FileHandler.readFromFile());
        panelGrid.refresh();
    }//GEN-LAST:event_menuFileOpenActionPerformed

    private void menuFileSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFileSaveActionPerformed
        FileHandler.writeToFile(te.getPolygon(), false);
    }//GEN-LAST:event_menuFileSaveActionPerformed

    private void menuFileSaveAsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFileSaveAsActionPerformed
        FileHandler.writeToFile(te.getPolygon(), true);
    }//GEN-LAST:event_menuFileSaveAsActionPerformed

    private void menuEditSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuEditSelectActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuEditSelectActionPerformed

    private void menuEditDeselectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuEditDeselectActionPerformed
        te.getPolygon().clearSelection();
        panelGrid.refresh();
    }//GEN-LAST:event_menuEditDeselectActionPerformed

    private void menuEditDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuEditDeleteActionPerformed
        te.setPolygon(new Polygon());
        panelGrid.refresh();
    }//GEN-LAST:event_menuEditDeleteActionPerformed

    private void menuFileHoriActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFileHoriActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuFileHoriActionPerformed

    private void menuFileVertiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuFileVertiActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuFileVertiActionPerformed

    private void menuViewGridActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuViewGridActionPerformed
        panelGrid.switchShowGrid();
    }//GEN-LAST:event_menuViewGridActionPerformed

    private void menuViewScaleActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuViewScaleActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuViewScaleActionPerformed

    private void menuZoomInActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuZoomInActionPerformed
        panelGrid.zoomIn();
    }//GEN-LAST:event_menuZoomInActionPerformed

    private void menuZoomOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuZoomOutActionPerformed
        panelGrid.zoomOut();
    }//GEN-LAST:event_menuZoomOutActionPerformed

    private void menuToolsAutoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuToolsAutoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuToolsAutoActionPerformed

    private void menuToolsFillActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuToolsFillActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_menuToolsFillActionPerformed

    private void menuToolsStickActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuToolsStickActionPerformed
        panelGrid.setStickGrid(((JCheckBoxMenuItem)evt.getSource()).getState());
    }//GEN-LAST:event_menuToolsStickActionPerformed

    private void btnDrawActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnDrawActionPerformed
        btnSelect.setSelected(false);
        btnLine.setSelected(false);
        panelGrid.setMode(Mode.DRAW);
    }//GEN-LAST:event_btnDrawActionPerformed

    private void btnSelectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSelectActionPerformed
        btnDraw.setSelected(false);
        btnLine.setSelected(false);
        panelGrid.setMode(Mode.SELECT);
    }//GEN-LAST:event_btnSelectActionPerformed

    private void btnLineActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLineActionPerformed
        btnDraw.setSelected(false);
        btnSelect.setSelected(false);
        panelGrid.setMode(Mode.LINE);
    }//GEN-LAST:event_btnLineActionPerformed

    private void panelGridMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_panelGridMouseMoved
        lblCoordinates.setText("x:" + (int)(evt.getX() / panelGrid.getScale()) + " y:" + (int)(evt.getY() / panelGrid.getScale()));
    }//GEN-LAST:event_panelGridMouseMoved

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditorForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditorForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JToggleButton btnDraw;
    private javax.swing.JToggleButton btnLine;
    private javax.swing.JToggleButton btnSelect;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JLabel lblCoordinates;
    private javax.swing.JMenu menuEdit;
    private javax.swing.JMenuItem menuEditDelete;
    private javax.swing.JMenuItem menuEditDeselect;
    private javax.swing.JMenuItem menuEditSelect;
    private javax.swing.JMenu menuFile;
    private javax.swing.JMenuItem menuFileHori;
    private javax.swing.JMenuItem menuFileNew;
    private javax.swing.JMenuItem menuFileOpen;
    private javax.swing.JMenuItem menuFileSave;
    private javax.swing.JMenuItem menuFileSaveAs;
    private javax.swing.JMenuItem menuFileVerti;
    private javax.swing.JMenu menuTools;
    private javax.swing.JCheckBoxMenuItem menuToolsAuto;
    private javax.swing.JCheckBoxMenuItem menuToolsFill;
    private javax.swing.JCheckBoxMenuItem menuToolsStick;
    private javax.swing.JMenu menuView;
    private javax.swing.JCheckBoxMenuItem menuViewGrid;
    private javax.swing.JCheckBoxMenuItem menuViewScale;
    private javax.swing.JMenu menuZoom;
    private javax.swing.JMenuItem menuZoomIn;
    private javax.swing.JMenuItem menuZoomOut;
    private editor.gui.DrawingPanel panelGrid;
    // End of variables declaration//GEN-END:variables
}
